<html>
<head><meta charset="utf-8"><title>clone and auto-traits · t-compiler/wg-rfc-2229 · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/index.html">t-compiler/wg-rfc-2229</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html">clone and auto-traits</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="235261190"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/235261190" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rocksand <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#235261190">(Apr 19 2021 at 22:46)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span>  I noticed some issues around providing warning for the <code>Clone</code> trait. We get back a lot of positives, especially on closures for which we don't actively attempt to clone. For instance this code, will trigger a warning:</p>
<div class="codehilite"><pre><span></span><code>struct Foo(i32);
impl Drop for Foo {
    fn drop(&amp;mut self) {
        println!(&quot;{:?} dropped&quot;, self.0);
    }
}
fn test5_significant_drop_non_drop_aggregate() {
    let t = (Foo(0), 0i32);

    let c = || {
        let _t = t.1;
    };

    c();
}
</code></pre></div>
<p>My question is, do we want to provide this many warnings? As it is right now, the warnings could lead to a lot of clutter because many of those warnings are unlikely to be useful to the user.</p>



<a name="235293257"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/235293257" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rocksand <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#235293257">(Apr 20 2021 at 06:22)</a>:</h4>
<p>I am realizing now that I just pasted the wrong snippet of code <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="235293862"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/235293862" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rocksand <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#235293862">(Apr 20 2021 at 06:29)</a>:</h4>
<p>I meant something like this:<br>
‘’’<br>
struct Foo(Bar, i32);<br>
impl Clone for Foo { ...}</p>
<p>fn main() {<br>
let t = Foo(Bar(0), 0);<br>
let c = || {<br>
let t = t.0;<br>
}<br>
}<br>
‘’’</p>



<a name="235356083"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/235356083" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#235356083">(Apr 20 2021 at 14:50)</a>:</h4>
<p><span class="user-mention" data-user-id="307184">@rocksand</span> interesting that the warnings are so common</p>



<a name="235356109"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/235356109" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#235356109">(Apr 20 2021 at 14:50)</a>:</h4>
<p>is it only <code>Clone</code> that has so many false warnings?</p>



<a name="235356121"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/235356121" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#235356121">(Apr 20 2021 at 14:50)</a>:</h4>
<p>hmm</p>



<a name="235356127"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/235356127" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#235356127">(Apr 20 2021 at 14:50)</a>:</h4>
<p>I have some thoughts on how we could narrow it down</p>



<a name="235755180"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/235755180" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rocksand <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#235755180">(Apr 22 2021 at 22:01)</a>:</h4>
<p>I was able to add <code>send_trait()</code> but I am struggling with adding <code>unwind_safe_trait()</code> and <code>unwind_safe_ref_trait()</code>. When I call the unwind safe functions it doesn't give me back any DefId. I am not sure what else I need to do apart from adding <code>#[lang = ...]</code> in this file <a href="https://github.com/rust-lang/rust/blob/master/library/std/src/panic.rs#L139">https://github.com/rust-lang/rust/blob/master/library/std/src/panic.rs#L139</a></p>



<a name="235844198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/235844198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#235844198">(Apr 23 2021 at 13:56)</a>:</h4>
<p><span class="user-mention" data-user-id="307184">@rocksand</span> can you send me your branch?</p>



<a name="235861160"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/235861160" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rocksand <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#235861160">(Apr 23 2021 at 15:28)</a>:</h4>
<p>Here is the branch: <a href="https://github.com/sexxi-goose/rust/tree/rox-auto-trait">https://github.com/sexxi-goose/rust/tree/rox-auto-trait</a></p>



<a name="236235214"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/236235214" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#236235214">(Apr 26 2021 at 20:08)</a>:</h4>
<p><span class="user-mention" data-user-id="307184">@rocksand</span> one thought I had is that this is in libstd; in some cases, libstd may not be available</p>



<a name="236235255"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/236235255" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#236235255">(Apr 26 2021 at 20:09)</a>:</h4>
<p>in general you should try to do something sensible if the lang-iteam is not present</p>



<a name="236235422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/236235422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#236235422">(Apr 26 2021 at 20:10)</a>:</h4>
<p>see e.g. <a href="https://github.com/sexxi-goose/rust/commit/e7c0f4c66302a3f1c158eed0590943b67ccba349#r50028344">this comment</a></p>



<a name="236235466"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/236235466" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#236235466">(Apr 26 2021 at 20:11)</a>:</h4>
<p>basically something like this</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">unwind_safe_obligation_should_hold</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tcx</span><span class="p">.</span><span class="n">lang_items</span><span class="p">().</span><span class="n">unwind_safe_trait</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">.</span><span class="n">map</span><span class="p">(</span><span class="o">|</span><span class="n">unwind_safe</span><span class="o">|</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">ty_contains_trait</span><span class="p">(</span><span class="n">ty</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">cause</span><span class="p">,</span><span class="w"> </span><span class="n">unwind_safe</span><span class="p">))</span><span class="w"></span>
<span class="w">    </span><span class="p">.</span><span class="n">unwrap_or</span><span class="p">(</span><span class="kc">false</span><span class="p">)</span><span class="w"></span>
</code></pre></div>



<a name="237712179"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/237712179" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rocksand <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#237712179">(May 06 2021 at 19:03)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span>  Are you familiar with this error? I can't seem to figure out what I need to do to solve the errors. <a href="https://github.com/rust-lang/rust/pull/84730#issuecomment-829720950">https://github.com/rust-lang/rust/pull/84730#issuecomment-829720950</a></p>



<a name="237857988"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/237857988" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#237857988">(May 07 2021 at 17:11)</a>:</h4>
<p><span class="user-mention" data-user-id="307184">@rocksand</span> I thnk you need this;</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[cfg_attr(not(test), lang = </span><span class="s">"unwind_safe"</span><span class="cp">)]</span><span class="w"></span>
</code></pre></div>
<p>the reason is that when you are running tests, it builds a second copy of the stdlib, and in that copy you want to omit the traits</p>



<a name="237857997"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/237857997" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#237857997">(May 07 2021 at 17:11)</a>:</h4>
<p>kind of a hack</p>



<a name="237899848"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/237899848" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rocksand <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#237899848">(May 07 2021 at 22:32)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span>  I tried doing that but as a result I get an error at a different location:</p>
<div class="codehilite"><pre><span></span><code>error[E0522]: definition of an unknown language item: `unwind_safe`
   --&gt; library/std/src/panic.rs:136:23
    |
136 | #[cfg_attr(not(test), lang = &quot;unwind_safe&quot;)]
    |                       ^^^^^^^^^^^^^^^^^^^^ definition of unknown language item `unwind_safe`

error: aborting due to previous error
</code></pre></div>



<a name="237983245"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/237983245" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#237983245">(May 08 2021 at 20:42)</a>:</h4>
<p><em>that</em> sounds like a bootstrap problem</p>



<a name="237983270"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/237983270" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#237983270">(May 08 2021 at 20:42)</a>:</h4>
<p>maybe this, <span class="user-mention" data-user-id="307184">@rocksand</span> ?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[cfg_attr(all(not(test), not(bootstrap)), lang = </span><span class="s">"unwind_safe"</span><span class="cp">)]</span><span class="w"></span>
</code></pre></div>



<a name="237983281"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/237983281" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#237983281">(May 08 2021 at 20:43)</a>:</h4>
<p>bootstrap is when the compiler builds itself with the current beta; since the lang item isn't in the current beta, this gets an error</p>



<a name="237983283"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/237983283" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#237983283">(May 08 2021 at 20:43)</a>:</h4>
<p>so this says to skip that attribute</p>



<a name="238053950"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/238053950" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rocksand <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#238053950">(May 09 2021 at 18:57)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span>  Oh ok, I couldn't figure out how to combine both <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span> Your suggestion worked! I think I addressed all the comments you left on my PR</p>



<a name="238141569"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/238141569" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#238141569">(May 10 2021 at 13:28)</a>:</h4>
<p><span class="user-mention" data-user-id="307184">@rocksand</span> left a few more comments</p>



<a name="238356884"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/clone%20and%20auto-traits/near/238356884" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rocksand <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/clone.20and.20auto-traits.html#238356884">(May 11 2021 at 18:06)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span>  Just added the helper function. Let me know if there is any thing you would like me to change</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>